package com.ctz.commonframe.utils;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

public final class Logger {
    public static boolean flagShowLog = true; //是否显示log
    public static boolean flagWrite2File = false; //是否输出到文件
    private static LogWriter writer;
    private static Context mContext;
    
    public static void init(Context context){
    	mContext = context;
    	writer = LogWriter.getInstance(context);
		writer.startRun();
    }
    
    public static void enableLog(boolean enable){
    	flagShowLog = enable;
    }
    
    public static void enableWrite2File(boolean enable){
    	flagWrite2File = enable;
    	if(writer.isRunning()){
    		if(!enable)
    			writer.stopRun();
    	}else{
    		if(enable)
    			writer.startRun();
    	}
    }
    
    public static void e (String tag, String msg){
    	if(flagShowLog){
    		Log.e(TextUtils.isEmpty(tag) ? mContext.getPackageName() : tag, msg);
    	}
    	if(flagWrite2File){
    		String content = buildLogString(TextUtils.isEmpty(tag) ? mContext.getPackageName() : tag, msg);
    		writer.addData(content);
    	}
    	
    }
    
    public static void e(String msg){
    	e(null, msg);
    }
    
    public static void d (String tag, String msg){
    	if(flagShowLog){
    		Log.d(TextUtils.isEmpty(tag) ? mContext.getPackageName() : tag, msg);
    	}
    	if(flagWrite2File){
    		String content = buildLogString(TextUtils.isEmpty(tag) ? mContext.getPackageName() : tag, msg);
    		writer.addData(content);
    	}
    }
    
    public static void d(String msg){
    	d(null, msg);
    }
    
    private static String buildLogString(String tag, String msg){
    	String date = DateUtil.millis2String(System.currentTimeMillis(), DateUtil.DATE_FORMAT_YYYY_MM_DD + DateUtil.TIME_FORMAT_HH_MM_SS);
    	return date + "\t" +  tag + "\t" + msg;
    }
    
    

}
